/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caa.struts.login.action;

import caa.struts.login.form.LoginForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import th.com.entity.UserEntity;
import th.com.ku.CAAService;

/**
 *
 * @author Ketsaraporn
 */
public class LoginAction extends DispatchAction{

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private final static String FAILURE = "failure";

    public ActionForward doLogin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LoginForm formBean = (LoginForm) form;
        String UserName = formBean.getUserName();
        String UserPassword = formBean.getUserPassword();

        UserEntity entity = CAAService.selectLogin(UserName, UserPassword);
        if ((entity == null)) 
            {
//   Map session = ActionContext.getContext().getSession();
//   session.put("logged-in","true");
//   return SUCCESS;
//   }
//else
            formBean.setError();
           
            return mapping.findForward(FAILURE);
        }
//        System.out.println(entity.getUseId());
        request.getSession(true).setAttribute("isUsername", entity);
        return mapping.findForward(SUCCESS);
    }
    
    public ActionForward doLogout(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession(true).removeAttribute("isUsername");
        return mapping.findForward(FAILURE);
    }
}
